/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.mapping;

import java.util.*;
import javax.swing.*;
import org.jdesktop.beansbinding.*;
import ch.trackedbean.loaders.lists.*;

/**
 * {@link ISubBinding} for the list managment of an components holding an element from a model and an external list (like {@link JComboBox}).<br>
 * The list content is loaded using {@link ListManager}.<br>
 * Just set this object via {@link BindingInfos#setSubBinding(ISubBinding)} on the corresponding binding.
 * 
 * @author M. Hautle
 */
public class ListBindingInfos implements ISubBinding {
	/** The binding to the destination component. */
	protected final Binding binding;

	/** The content type of the list. This information is used for the load of the content list. */
	protected Class<?> type;

	/**
	 * Additional string specifying the "subtype" to load (may be null). This information is used for the load of the content list.
	 */
	protected String subType;

	/** Additional settings for the load (may be null). This information is used for the load of the content list. */
	protected Map<Object, Object> settings;

	/** The converter to use for the list entries or null */
	protected Converter listConverter;

	/**
	 * Default constructor
	 * 
	 * @param binding The binding to control
	 * @param type The content type of the list
	 * @param subType Additional string specifying the "subtype" to load (may be null)
	 * @param settings Additional settings for the load (may be null)
	 */
	public ListBindingInfos(Binding binding, Class<?> type, String subType, Map<Object, Object> settings) {
		this.binding = binding;
		this.type = type;
		this.subType = subType;
		this.settings = settings;
	}

	/**
	 * Returns the content type of the list.<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @return Returns the type.
	 */
	public Class<?> getType() {
		return type;
	}

	/**
	 * Sets the content type of the list.<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @param type The type to set
	 */
	public void setType(Class<?> type) {
		this.type = type;
	}

	/**
	 * Returns an additional string specifying the "subtype" of the list (may be null).<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @return Returns the subType.
	 */
	public String getSubType() {
		return subType;
	}

	/**
	 * Sets an additional string specifying the "subtype" of the list (may be null).<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @param subType The subType to set, may be <code>null</code>
	 */
	public void setSubType(String subType) {
		this.subType = subType;
	}

	/**
	 * Returns the additional settings for the list (may be null).<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @return Returns the settings.
	 */
	public Map<Object, Object> getSettings() {
		return settings;
	}

	/**
	 * Sets the additional settings for the list (may be null).<br>
	 * This property is used to call {@link ListManager#getList(Class, String, Map)}.
	 * 
	 * @param settings The settings to set, may be <code>null</code>
	 */
	public void setSettings(Map<Object, Object> settings) {
		this.settings = settings;
	}

	/**
	 * Returns the converter used for the source list of the component.
	 * 
	 * @return Returns the listConverter.
	 */
	public Converter getListConverter() {
		return listConverter;
	}

	/**
	 * Sets the converter used for the source list of the component.
	 * 
	 * @param listConverter The listConverter to set, may be <code>null</code>
	 */
	public void setListConverter(Converter listConverter) {
		this.listConverter = listConverter;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void bind(BindingInfos info) {
		binding.setConverter(listConverter);
		// set the content list
		binding.setSourceObject(ListManager.ME.getList(type, subType, settings));
		binding.bind();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void unbind(BindingInfos info) {
		if (binding.isBound())
			binding.unbind();
		binding.setSourceObject(null);
	}
}
